<?php

namespace app\member\service;

use app\member\model\Routers;
use app\member\model\Users;

class RouterService
{

    public function getUserMenu($uid){
        try{
            $db = new Users();
            $routerStr = $db->
                alias("a")->
                leftjoin("groups b","a.group_id = b.id")->
                where('users.id',$uid)->
                value("routers");
            $routerArr = explode(",",$routerStr);
            $router = new Routers();
            $routers = $router->
                where("id",'in',$routerArr)->
                where('is_delete',0)->
                where("is_show",1)->select()->toArray();
            $tree = getTree($routers);
            return AjaxReturn(0, "", $tree);
        }catch (\Throwable $ex){
            return AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage());
        }
    }

    public function getBottomRouter($ids){
        try{
            $data = [];
            $db = new Routers();
            foreach ($ids as $v){
                $pid = $db->where('id',$v)->value("parent_id");
                if($pid == 0 || $pid == null){

                }else{
                    array_push($data,$v);
                }
            }
            return AjaxReturn(0, "", $data);
        }catch (\Throwable $ex){
            return AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage());
        }
    }

    public function treeList(){
        try{
            $db = new Routers();
            $data = $db->
                where("is_delete",0)->
                where("is_show",1)->
                select()->toArray();

            $tree = getTree($data);
            return AjaxReturn(0, "", $tree);
        }catch (\Throwable $ex){
            return AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage());
        }
    }

    /**
     * Notes:分页查询
     * User: ZHY
     * DateTime: 2023/7/5 21:02
     * @param $page
     * @param $limit
     * @param $where
     * @return array
     */
    public function list($page = 1, $limit = 10, $where = [])
    {
        try {
            array_push($where, ['routers.is_delete', '=', 0]);
            $offset = ($page - 1) * $limit;
            $db = new Routers();
            $li = $db->
                alias("a")->
                leftjoin("routers b","a.parent_id = b.id")->
                where($where)->
                limit($offset, $limit)->
                order("id")->
                field("a.*,b.router_name as parent_name")->
                select();
            $count = $db->where($where)->count();
            return AjaxReturn(0, "", $li, $count);
        } catch (\Throwable $ex) {
            return AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage());
        }
    }

    /**
     * Notes:添加
     */
    public function add($data)
    {
        try {
            $db = new Routers();
            $f = $db->where("router_name",$data["router_name"])->
            where("is_delete",0)->find();
            if($f){
                return AjaxReturn(-1,"路由名称已存在");
            }

            $res = $db->insert($data);
            if ($res) {
                return AjaxReturn(0, '添加成功');
            } else {
                return AjaxReturn(-1, '添加失败');
            }
        } catch (\Throwable $ex) {
            return AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage());
        }
    }

    public function softdelete($ids)
    {
        try {
            $db = new Routers();
            $res = $db->where('id', 'in', $ids)->update(['is_delete' => 1]);
            if ($res == count($ids)) {
                return AjaxReturn(0, '删除成功');
            } else {
                return AjaxReturn(-1, '删除失败');
            }
        } catch (\Throwable $ex) {
            return AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage());
        }
    }

    public function edit($data)
    {
        try {
            if($data['id'] == 0){
                return AjaxReturn(-1, '不可修改');
            }
            $db = new Routers();
            $f = $db->where("router_name",$data['router_name'])->where('is_delete',0)
                ->where('id','<>',$data['id'])->find();
            if($f){
                return AjaxReturn(-1, '路由名称冲突');
            }
            $db->update($data);
            return AjaxReturn(0, '操作成功');
        } catch (\Throwable $ex) {
            return AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage());
        }
    }
}